import { Config } from "@/config/Config";
import { HttpClient } from "@/httpclient/HttpClient";
import { DateUtil } from "@/utils/DateUtil";
import { Logger } from "@/utils/Logger";
import { RandomUtil } from "@/utils/RandomUtil";

export class ZoneModel {


    public static Status = {
        Recomend: 1,
        Fire: 2,
        NowOpen: 3,
    }

    public static StatusNameMap: Map<number, string> = new Map<number, string>()
        .set(ZoneModel.Status.Recomend, "推荐")
        .set(ZoneModel.Status.Fire, "火爆")
        .set(ZoneModel.Status.NowOpen, "刚开一秒")

    public serverId: number;
    public serverName: string;
    public channelId:number;
    public onlineNum: number;
    public neiguaNum: number;
    public registerNum: number;
    public status: number;
    public open_server_time: number;
    public ios_view_status: number;
    public android_view_status: number;


    public static selectList: Array<ZoneModel> = []; //下拉框服务器列表
    public static modelList: Array<ZoneModel> = [];

    public static totalNum: number = 100;

    public static initTest() {
        this.modelList = [];
        for (let i = 0; i < this.totalNum; i++) {
            let model: ZoneModel = new ZoneModel();
            model.serverId = i;
            model.serverName = "服务器" + i
            model.channelId = RandomUtil.nextInt(1, 4)
            model.onlineNum = RandomUtil.nextInt(1, 500);
            model.registerNum = RandomUtil.nextInt(1, 500);
            model.neiguaNum = RandomUtil.nextInt(1, 500)
            model.status = RandomUtil.nextInt(1, 3);
            model.open_server_time = DateUtil.now();
            model.ios_view_status = 1;
            model.android_view_status = 1;
            this.modelList[i] = model;
        }
    }

    public static json2SelectList(data: Array<object>) {
        this.selectList = [];
        for (let i = 0; i < data.length; i++) {
            let model: ZoneModel = new ZoneModel();
            model.serverId = data[i]["serverId"]
            model.serverName = data[i]["serverName"]
            model.channelId = data[i]["channel_id"]
            this.selectList[i] = model;
        }
        Logger.log("json2SelectList===", this.selectList)
    }

    public static json2ModelList(jsonData: object) {
        this.totalNum = jsonData["total_num"];
        let data: Array<object> = jsonData["data"]
        this.modelList = [];
        for (let i = 0; i < data.length; i++) {
            let model: ZoneModel = new ZoneModel();
            model.serverId = data[i]["server_id"]
            model.channelId = data[i]["channel_id"]
            model.serverName = data[i]["server_name"]
            model.neiguaNum = data[i]["auto_online_num"];
            model.onlineNum = data[i]["online_num"];
            model.status = data[i]["server_status"];
            model.open_server_time = data[i]["open_server_time"] * 1000;
            model.registerNum = data[i]["role_num"]
            model.ios_view_status = data[i]["ios_view_status"]
            model.android_view_status = data[i]["android_view_status"]
            this.modelList[i] = model;
        }
    }


    public static getShowList(channelId: number, listQuery: any, callback: Function) {
        let showList: Array<ZoneModel> = [];
        if (Config.isConnectServer) {
            HttpClient.queryServerList(channelId, listQuery["page"], listQuery["limit"], () => {
                this.modelList.sort((a: ZoneModel, b: ZoneModel) => {
                    if (listQuery.sort == "+id") {
                        return a.serverId - b.serverId;
                    } else if (listQuery.sort == "-id") {
                        return b.serverId - a.serverId;
                    }
                    else {
                        return a.serverId - b.serverId;
                    }
                });
                showList = this.modelList
                callback && callback(showList);
            })
        } else {
            let startIndex: number = (listQuery["page"] - 1) * listQuery["limit"]
            let endEndex: number = startIndex + listQuery["limit"]
            for (let i = startIndex; i < endEndex; i++) {
                let model: ZoneModel = this.modelList[i];
                if (model) {
                    showList.push(model)
                }
            }
            Logger.log("ZoneModel.getShowList", ZoneModel.modelList)
            ZoneModel.totalNum = ZoneModel.modelList.length
            callback && callback(showList);
        }
    }

    public static sureEditZone(model: any) {
        for (let i = 0; i < this.modelList.length; i++) {
            if (this.modelList[i].serverId == model.serverId) {
                this.modelList[i].neiguaNum = model.neiguaNum;
                this.modelList[i].onlineNum = model.onlineNum;
                this.modelList[i].registerNum = model.registerNum;
                this.modelList[i].serverId = model.serverId;
                this.modelList[i].serverName = model.serverName;
                this.modelList[i].status = parseInt(model.status);
                this.modelList[i].ios_view_status = parseInt(model.ios_view_status)
                this.modelList[i].android_view_status = parseInt(model.android_view_status)
                HttpClient.editServerStatus(this.modelList[i].serverId, this.modelList[i].status, this.modelList[i].ios_view_status, this.modelList[i].android_view_status)
                break;
            }
        }
    }

}